package view.controller;

import common.Message;
import common.User;
import dao.GetUser;
import dao.modifypwd;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import server.ClientConnServer;
import utils.JDBC;
import utils.MD5;
import view.Login;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.regex.Pattern;

public class FindBackPsw {
    public TextField getID;
    public TextField getEmail;
    public PasswordField getPsw;
    public PasswordField getAgainPsw;
    public Button sureButton;
    public Button returnButton;
    Pattern password_pattern =Pattern.compile("[^ [A-Za-z0-9]+$]{6,16}");

    public void surebt(){
        User user = new User();
        Connection conn = null;
        ResultSet res = null;
        user.setId(getID.getText());
        Properties info=new Properties();
        try {
            info.load(new FileInputStream("src\\sql.properties"));
            String user1=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn = DriverManager.getConnection(url, user1, password);
            String sql = "select * from users where userid = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, user.getId());
            res = presta.executeQuery();
            if (res.next()) {
                user.setEmail(res.getString("email"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = new Message();
        msg.setUser(user);
        if(getID.getText().equals("")){
            JOptionPane.showMessageDialog(null, "账号不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(getEmail.getText().equals("")){
            JOptionPane.showMessageDialog(null, "邮箱不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(getPsw.getText().equals("")){
            JOptionPane.showMessageDialog(null, "密码不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(!password_pattern.matcher(getPsw.getText()).matches()){
            JOptionPane.showMessageDialog(null, "密码格式错误！", "提示", JOptionPane.ERROR_MESSAGE);
        } else if(getAgainPsw.getText().equals("")){
            JOptionPane.showMessageDialog(null, "重复密码不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(!getAgainPsw.getText().equals(getPsw.getText())){
            JOptionPane.showMessageDialog(null, "两次密码不一致！", "提示", JOptionPane.ERROR_MESSAGE);
        }
//        else if(new ClientConnServer().sendFindBackPswToServer(msg)){
//            JOptionPane.showMessageDialog(null, "邮箱不存在！", "提示", JOptionPane.ERROR_MESSAGE);
//        }
        else if(!getEmail.getText().equals(user.getEmail())){
            JOptionPane.showMessageDialog(null, "邮箱和账号不匹配！", "提示",JOptionPane.ERROR_MESSAGE);
        }else{
//            Message msg1 =new Message();
//            msg1.setType("mpwd");
            User u=new User();
            u.setEmail(getEmail.getText());
            MD5 md5 = new MD5();
//            u.setPsw(md5.generateMD5(getPsw.getText()));
            boolean flag = new modifypwd().mpwd(getEmail.getText(),md5.generateMD5(getPsw.getText()));
//            msg.setUser(u);
            if(flag){
                JOptionPane.showMessageDialog(null, "修改成功", "提示",JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage) getPsw.getScene().getWindow();
                stage.close();
                Login rj=new Login();
                try {
                    rj.start(stage);
//                        rj.Show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                JOptionPane.showMessageDialog(null, "修改失败", "提示",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void returnbt(){
        Stage stage = (Stage)returnButton.getScene().getWindow();
        stage.close();
        Login rj = new Login();
        try{
            rj.start(stage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
